<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="management" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                    <el-dropdown-item @click.native="hEdit('') ">编辑部门</el-dropdown-item>
                    <el-dropdown-item>删除部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree icon-class="false" :default-expand-all="true" :data="list" :props="defaultProps">
          <template slot-scope="{ data,node }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <svg-icon v-if="data.children.length === 0" icon-class="management" />
                <svg-icon v-if="data.children.length !== 0" :icon-class="node.expanded?'jian':'jia' " />

                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id) ">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length ===0" @click.native="deleteSection(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <el-dialog
          :title="isEdit?'编辑':'添加'"
          :visible.sync="showDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          :before-close="handleClose"
          @close="hDialogClose"
        >
          <addor-edit :id="curId" ref="refEdit" :old-list="oldList" :is-edit="isEdit" @success="hSuccess" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
import { tranListToTreeData } from '@/utils'
import { getDepartments, delDepartment } from '@/api/departments'
import AddorEdit from './deptDialog.vue'
export default {
  components: { AddorEdit },
  data() {
    return {
      icon: 'el-icon-circle-plus-outline',
      oldList: [],
      list: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      curId: '',
      showDialog: false, // 是否显示弹层
      isEdit: false // 是否处于编辑状态
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      try {
        const res = await getDepartments()
        // 保存数据
        // shift会取出数组第一个元素，同时会改变源数组
        const company = res.data.depts.shift()
        console.log('loadDepartments', company)
        this.oldList = res.data.depts
        this.list = tranListToTreeData(res.data.depts)
        this.list = this.list.filter(item => {
          return item.name != null
        })
      // 后端返回的数据中 第一条是公司信息，它应该直接就是tree的根节点
      // 为了后面计算方便，把它直接取出来
      } catch (err) {
        console.log(err)
      }
    },
    hAdd(id) {
      this.showDialog = true
      this.curId = id
    },
    hEdit(id) {
      // 1. 弹层出来
      this.showDialog = true
      // 2. 表单中要显示详情数据
      // 把id传给子组件，在子组件中去获取详情
      this.curId = id
      // 3. 传递一个标识给子组件，告诉它我们现在是编辑状态
      this.isEdit = true
      this.$nextTick(function() {
        console.log(this.$refs.refEdit, 6666666)
        this.$refs.refEdit.loadDetail()
      }
      )
    },
    hSuccess() {
      // 关闭弹层，再次更新数据
      this.showDialog = false
      this.loadDepartments()
      //  传递一个标识给子组件，告诉它我们现在是编辑状态
      this.isEdit = true
    },
    handleClose(done) {
      done()
      this.$refs.refEdit.form = {}
    },
    async delDepartmentFn(id) {
      const res = await delDepartment(id)
      console.log(res)
    },
    // dialog的close事件
    hDialogClose() {
      console.log('close')
      this.$refs.refEdit.resetForm()
    },
    // 删除部门
    deleteSection(id) {
      this.delDepartmentFn(id)
      this.loadDepartments()
    }

  }
}
</script>
